package com.wm.blog_admin.controller;


import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.query.TRoleQuery;
import com.wm.blog_admin.service.TRoleService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TRoleDO;
import com.wm.blog_common.entity.TRole;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/blog/role")
public class TRoleController extends AdminBaseController {

    @Autowired
    private TRoleService tRoleService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TRoleDO> get(@PathVariable Long id) {
        return tRoleService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tRoleService.deleteById(id);
    }

    /**
     * 添加
     * @param tRole
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TRole tRole){
        return tRoleService.insert(tRole);
    }

    /**
     * 修改
     * @param tRole
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TRole tRole){
        return tRoleService.update(tRole);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result page(Integer current, Integer size, TRoleQuery query){
        current = Optional.ofNullable(current).orElse(1);
        size = Optional.ofNullable(size).orElse(10);
        query = Optional.ofNullable(query).orElse(new TRoleQuery());
        return  tRoleService.page(new QueryPage(current, size), query);
    }


    /**
     * 权限管理   角色和菜单权限挂钩
     * @return
     */
    @PostMapping("/bindMenu")
    public Result bindMenu(@RequestBody TRoleQuery query){
        return tRoleService.bindMenu(query);
    }





    /**
     * 权限管理   角色和菜单权限挂钩
     * @return
     */
    @GetMapping("/roleMenu")
    public Result roleMenu(TRoleQuery query){
        return tRoleService.roleMenu(query);
    }

    /**
     * 权限管理   查询所有未禁用的角色数据
     * @return
     */
    @GetMapping("/list")
    public Result list(TRoleQuery query,HttpServletRequest request){
        return tRoleService.list(query);
    }


    /**
     * 角色增加/修改
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TRoleQuery query, HttpServletRequest request){
        query =  Optional.ofNullable(query).orElse(new TRoleQuery());
        query.setAdminId(super.currentAdminId(request));
        return tRoleService.saveOrUpdate(query);
    }







}
